import $API from '@/api/services'

const goods = {
  state: {
    goodsItem: null,
    goodsScore: null,
    checkoutGoodsInfo: 0, // 结算的商品信息
    goodsCart: {
      count: 0,
      list: [],
      hasGoods: false
    },
    cartList: null,
    goodsNum: 0
  },
  actions: {
    _getGoodsData({ commit, state }, id) {
      $API('getProductInfo', [id]).then(res => {
        if (res.data) {
          this.dispatch('getGoodsScore', res.data.productid)
          commit('SAVE_GOODS_ITEM', res.data)
        }
      })
    },
    // 获取单个商品数据
    fetchGoodsData({ commit, state }, id) {
      if(!state.goodsItem) {
        this.dispatch('_getGoodsData', id)
      } else {
        if(state.goodsItem.productid != id) {
          this.dispatch('_getGoodsData', id)
        }
      }
    },
    // 获取商品评分
    getGoodsScore({ commit }, productId) {
      $API('getProductGrade', [productId]).then(res => {
        if (res.data) {
          // 获取商品评分
          commit('SAVE_GOODS_SCORE', res.data)
        }
      })
    },
    // 商品评分
    orderRating({ commit }, opts) {
      $API('addProductGrade', [opts]).then(res => {
        if (res) {
          $VM.$vgo.tip('评论成功', 'success')
          this.dispatch('getGoodsScore', opts.productId)
        }
      })
    },
    // 商品加入购物车
    addToCart({ commit, state }, obj) {
      $API('addToCart', [obj.productid, obj.num]).then(res => {
        if (res) {
          $VM.$vgo.tip('商品添加成功', 'success')
          // 如果之前购物车被清空了就重新获取
          // if (!state.cartList) {
            this.dispatch('fetchCartGoods')
          // }

          commit('ADD_TO_CART', { id: obj.productid, num: obj.num})
        }
      })
    },
    // 获取购物车列表
    fetchCartGoods({ commit }) {
      let userId = this.getters.panoInfo ? this.getters.panoInfo.user_id : ''
      if(!userId) return
      $API('getCartList', [1, 10, userId]).then(res => {
        if (res.data.list.length > 0) {
          commit('CART_DATA', res.data)
        }
      })
    },
    // 更新购物车的商品数量
    updateCartGoods({ commit }, cartids) {
      commit('UPDATE_CART_GOODS', cartids)
    },
    // 更新商品数量
    updateCartGoodsNum({ commit }, ary) {
      commit('UPDATE_CART_GOODS_NUM', ary)

      $API('updateCart', ary)
    },
    // 删除购物车商品
    deleteCartGoods({ commit }, cartId) {
      let userId = 0
      if (this.getters && this.getters.userInfo) {
        userId = this.getters.userInfo.user_id
      }
      $API('deleteToCart', [cartId]).then(res => {
        if (res) {
          commit('DELETE_CART_GOODS', {userId: userId, cartId: cartId})
        }
      })
    },
    // 结账的商品信息
    checkoutGoods({ commit }, obj) {
      commit('CHECKOUT_GOODS_INFO', obj)
    },
  },
  mutations: {
    SAVE_GOODS_ITEM: (state, data) => {
      state.goodsItem = data
    },
    SAVE_GOODS_SCORE: (state, data) => {
      state.goodsScore = data
    },
    ADD_TO_CART: (state, obj) => {
      const hasGoods = (_id) => {
        return state.goodsCart.list.includes(_id)
      }

      if(state.goodsCart.list.length > 0) {
        if(!hasGoods(obj.id)) {
          state.goodsCart.list.push(obj.id)
        }
      } else {
        state.goodsCart.list.push(obj.id)
      }

      state.goodsCart.count++
      state.goodsCart.hasGoods = true
    },
    CART_DATA: (state, data) => {
      state.goodsCart.list = []
      data.list.map(item => {
        item.isSelected = true
        state.goodsCart.list.push(item.fk_productid)
      })
      state.cartList = data
      state.goodsCart.hasGoods = true
    },
    UPDATE_CART_GOODS: (state, cartids) => {
      state.cartList.list.map((item, idx) => {
        if (item.cartid == cartids[idx]) {
          state.cartList.splice(idx, 1)
        }
      })
      if (!state.cartList.length) {
        state.cartList = null
      }
    },
    UPDATE_CART_GOODS_NUM: (state, ary) => {
      state.cartList.list.map(item => {
        if (item.cartid == ary[0]) {
          item.num = ary[1]
        }
      })
    },
    DELETE_CART_GOODS: (state, obj) => {
      state.cartList.list.map((item, idx) => {
        if (item.cartid == obj.cartId) {
          state.cartList.list.splice(idx, 1)
          state.cartList.count--
          if(obj.userId) {
            // 购物车数量为0
            if (!state.cartList.count) {
              state.cartList = null
            }
          }
        }
      })
    },
    CHECKOUT_GOODS_INFO: (state, obj) => {
      state.checkoutGoodsInfo = obj
    }
  }
}

export default goods
